import unittest
import random
import requests


class XZS_cjcs(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.session = requests.Session()

    @classmethod
    def tearDownClass(cls):
        cls.session.close()

    def test_student_pagination(self):
        # 登录- 分页查询 - 增加 - id查询 - 编辑 - 删除
        a = {
            "userName": "admin",
            "password": "123456",
            "remember": False
        }
        # 发送POST请求进行登录操作
        rep1 = self.session.post("http://localhost:8000/api/user/login", json=a)
        self.assertEqual(200, rep1.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep1.json()['code'])
        # 分页查询
        a = {
            "userName": '',
            "role": 1,
            "pageIndex": 1,
            "pageSize": 10
        }
        # 发送POST请求进行学生分页查询操作
        rep2 = self.session.post("http://localhost:8000/api/admin/user/page/list", json=a)

        self.assertEqual(200, rep2.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep2.json()['code'])  # 断言返回参数中的code为1，表示成功

        # 增加
        rd = random.randint(1000,9999)
        a = {
            "userName": "a"+str(rd),
            "password": "123456",
            "realName": False,
            "role": 1,
            "status": 1,
            "age": 12,
            "sex": 1,
            "birthDay": '2000-01-01',
            "phone": '',
            "userLevel": 3
        }

        # 发送POST请求进行学生增加操作
        rep3 = self.session.post('http://localhost:8000/api/admin/user/edit', json=a)
        self.assertEqual(200, rep3.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep3.json()['code'])  # 断言返回参数中的code为1，表示成功
        print(rep3.json())
        # id查询
        rep4 = self.session.post(f"http://localhost:8000/api/admin/user/select/{rep3.json()['response']['id']}")
        self.assertEqual(200, rep4.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep4.json()['code'])  # 断言返回参数中的code为1，表示成功
        # 编辑
        a = {
            "id": rep3.json()['response']['id'],
            "userName": "asassa",
            "password": "123456",
            "realName": False,
            "role": 1,
            "status": 1,
            "age": 12,
            "sex": 1,
            "birthDay": '2000-01-01',
            "phone": '',
            "userLevel": 3
        }
        # 发送POST请求进行学生信息查询操作
        rep5 = self.session.post("http://localhost:8000/api/admin/user/edit", json=a)
        self.assertEqual(200, rep5.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep5.json()['code'])  # 断言返回参数中的code为1，表示成功
        # 删除
        rep6 = self.session.post(f"http://localhost:8000/api/admin/user/delete/{rep3.json()['response']['id']}")
        self.assertEqual(200, rep6.status_code)  # 断言响应状态码为200
        self.assertEqual(1, rep6.json()['code'])  # 断言返回参数中的code为1，表示成功


